<?php

    include("header.php");

    setTabIndex(2);

    createPage("Liga - Zilveren Toren");

?>



<?php



    $connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);

    mysql_select_db(DB_CATALOG);

    

    $dep = $_GET['dep'];



    $query = sprintf("select group_id from zt_group g

            join zt_tournament t on g.tournament_id = t.tournament_id

            where t.iscurrent = 1 and group_name = '%s'", $dep);

    $result = mysql_query($query, $connect);

    $row = mysql_fetch_assoc($result);

    $groupId = $row['group_id'];

    

    $result = mysql_query("select player_key, p.nom_prenom, p.elo_calcul, p.club, group_id, 

        played, won, drawn, lost, total, tpr, avg from zt_player_stat join playernew p

        on p.matricule = player_key where group_id = ".$groupId." order by total desc, played asc, 

        tpr desc, p.elo_calcul desc");



    $playerStats = array();

    while ($row = mysql_fetch_assoc($result)) {

        $playerStat = new PlayerStat;

        $playerStat->playerKey = $row['player_key'];

        $playerStat->groupId = $row['group_id'];

        $playerStat->playerName = $row['nom_prenom'];

        $playerStat->playerElo = $row['elo_calcul'];

        $playerStat->playerClub = $row['club'];

        $playerStat->played = $row['played'];

        $playerStat->won = $row['won'];

        $playerStat->drawn = $row['drawn'];

        $playerStat->lost = $row['lost'];

        $playerStat->total = $row['total'];

        $playerStat->tpr = $row['tpr'];

        $playerStat->avg = $row['avg'];

        array_push($playerStats, $playerStat);

    }

    

    $ratingPerformanceTable = array(

        "0.83"=>273,"0.66"=>117,"0.49"=>-7,"0.32"=>-133,"0.15"=>-296,

"0.99"=>677,"0.82"=>262,"0.65"=>110,"0.48"=>-14,"0.31"=>-141,"0.14"=>-309,

"0.98"=>589,"0.81"=>251,"0.64"=>102,"0.47"=>-21,"0.30"=>-149,"0.13"=>-322,

"0.97"=>538,"0.80"=>240,"0.63"=>95,"0.46"=>-29,"0.29"=>-158,"0.12"=>-336,

"0.96"=>501,"0.79"=>230,"0.62"=>87,"0.45"=>-36,"0.28"=>-166,"0.11"=>-351,

"0.95"=>470,"0.78"=>220,"0.61"=>80,"0.44"=>-43,"0.27"=>-175,"0.10"=>-366,

"0.94"=>444,"0.77"=>211,"0.60"=>72,"0.43"=>-50,"0.26"=>-184,"0.09"=>-383,

"0.93"=>422,"0.76"=>202,"0.59"=>65,"0.42"=>-57,"0.25"=>-193,"0.08"=>-401,

"0.92"=>401,"0.75"=>193,"0.58"=>57,"0.41"=>-65,"0.24"=>-202,"0.07"=>-422,

"0.91"=>383,"0.74"=>184,"0.57"=>50,"0.40"=>-72,"0.23"=>-211,"0.06"=>-444,

"0.90"=>366,"0.73"=>175,"0.56"=>43,"0.39"=>-80,"0.22"=>-220,"0.05"=>-470,

"0.89"=>351,"0.72"=>166,"0.55"=>36,"0.38"=>-87,"0.21"=>-230,"0.04"=>-501,

"0.88"=>336,"0.71"=>158,"0.54"=>29,"0.37"=>-95,"0.20"=>-240,"0.03"=>-538,

"0.87"=>322,"0.70"=>149,"0.53"=>21,"0.36"=>-102,"0.19"=>-251,"0.02"=>-589,

"0.86"=>309,"0.69"=>141,"0.52"=>14,"0.35"=>-110,"0.18"=>-262,"0.01"=>-677,

"0.85"=>296,"0.68"=>133,"0.51"=>7,"0.34"=>-117,"0.17"=>-273,

"0.84"=>284,"0.67"=>125,"0.50"=>0,"0.33"=>-125,"0.16"=>-284

    );



?>



<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">

	<tr>

		<td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">

			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">

				<tr><td>&nbsp;</td></tr>

			</table>

		</td>

		<td id="header" valign="top" width="100%">

			<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="40%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;
                          <span class="headerSmall">Huidige versie PLAYER.DBF: <?=CURRENT_PLAYER_DBF?>
                        <?php
                        if (isset($user) && $user->club_id == 100) {
                        ?>
                        <br/>
                        <a class="headerSmall" href="import_player.php">laad andere versie</a>
                        <?php
                        }
                        ?>
                        </span>
                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <?php include("inc_sublinks.php"); ?> 
                </tr>

				<tr><td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="../../assets/images/spacer.gif" alt="" height="1" width="1"></td></tr>

				<tr>

					<td>&nbsp;</td>

					<td valign="top">

						<table align="left" cellspacing="10" width="100%">

							<tr>

								<td>

                                    <span class="blackHeaderLarge">Zilveren Toren <?=CURRENT_ZT_YEAR?>

                                    - Afdeling <?=$dep?> - Spelersstatistieken</span>

                                    <br>&nbsp;<br>

                                    <?php

                                      $query = "select g.group_name from zt_group g join

                                        zt_tournament zt on zt.tournament_id = g.tournament_id

                                        where zt.isCurrent = 1";

                                      $result = mysql_query($query, $connect);

                                      while ($row = mysql_fetch_assoc($result)) {

                                          $groupName = $row['group_name'];

                                    ?>

                                    <a class="quicklink" href="zt_department.php?dep=<?=$groupName?>">Afdeling <?=$groupName?></a>|</li>

                                    <?php

                                      }

                                    ?>

                                    <br>&nbsp;<br>

                                    De TPR wordt berekend volgens <a href="http://www.fide.com/official/handbook.asp?level=B0210">de FIDE standaard</a>, d.w.z. dat

                                    er voor p=1 of p=0 geen TPR kan gegeven worden.

                                    <br>&nbsp;<br>

                                    <center><a href="zt_department.php?dep=<?=$dep?>"><img border="0" src="../../assets/images/24x24/shadow/arrow_left_green.gif"></a></center>

                                    <br>

                                    <table border="0" width="70%" cellspacing="0" cellpadding="2" align="center">

                                        <tr>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Rang</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Stamnr</strong></td>

                                            <td width="35%" style="border-bottom:#DDDDDD solid 1px"><strong>Naam</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Club</strong></td>

                                            <td width="10%" style="border-bottom:#DDDDDD solid 1px"><strong>Elo</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>W</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>R</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>V</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>#</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Res</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>TPR</strong></td>

                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>GEM</strong></td>

                                        </tr>

                                        <?php

                                            $index = 0;

                                            foreach ($playerStats as $item) {

                                                $index++;

                                                if ($index % 2 == 0) {

                                                    $css = "statTableData";

                                                } else {

                                                    $css = "statTableData2";

                                                }

                                                

                                                /**

                                                * A performance rating (TPR) for an event is calculated by taking 

                                                * (1) the rating of each player beaten and adding 400, 

                                                * (2) the rating of each player lost to and subtracting 400, 

                                                * (3) the rating of each player drawn, and 

                                                * (4) summing these figures and dividing by the number of games played

                                                * 

                                                * or Rp = Ra + 400 * (Wins - Losses) / Gms where Ra = average opponent ratings

                                                */

                                                

                                                $matches = array();

                                                

                                                $query = "select zp.player_key, p.elo_calcul, rt.result

                                                    from zt_player zp

                                                    join zt_player_list zpl on zpl.player_list_id = zp.player_list_id

                                                    join zt_tournament t on t.tournament_id = zpl.tournament_id

                                                    join zt_game_player zgp on zgp.index_nr_home = zp.index_nr

                                                    join zt_game g on g.game_id = zgp.game_id

                                                    join zt_player zp2 on zp2.index_nr = zgp.index_nr_visit

                                                    join zt_player_list zpl2 on zp2.player_list_id = zpl2.player_list_id

                                                    join playernew p on p.matricule = zp2.player_key

                                                    join zt_result_type rt on rt.result_type_id = zgp.result_type_id

                                                    where t.iscurrent = 1

                                                    and g.home_player_list_id = zpl.player_list_id

                                                    and zpl2.player_list_id = g.visit_player_list_id

                                                    and p.elo_calcul <> 0

                                                    and g.group_id = ".$groupId."

                                                    and zp.player_key = '".$item->playerKey."'";

                                                $homeMatchesResult = mysql_query($query, $connect);

                                                rejectTranIfError($query, "calculate_stats.php: selecteren van de thuispartijen is mislukt.");

                                                while ($homeMatchRow = mysql_fetch_assoc($homeMatchesResult)) {

                                                    $homeMatch = new Match;

                                                    $homeMatch->playerKey = $homeMatchRow['player_key'];

                                                    $homeMatch->opponentRating = $homeMatchRow['elo_calcul'];

                                                    $homeMatch->result = $homeMatchRow['result'];

                                                    $homeMatch->homeMatch = 1;

                                                    array_push($matches, $homeMatch);    

                                                }

                                                

                                                $query = "select zp.player_key, p.elo_calcul, rt.result

                                                    from zt_player zp

                                                    join zt_player_list zpl on zpl.player_list_id = zp.player_list_id

                                                    join zt_tournament t on t.tournament_id = zpl.tournament_id

                                                    join zt_game_player zgp on zgp.index_nr_visit = zp.index_nr

                                                    join zt_game g on g.game_id = zgp.game_id

                                                    join zt_player zp2 on zp2.index_nr = zgp.index_nr_home

                                                    join zt_player_list zpl2 on zp2.player_list_id = zpl2.player_list_id

                                                    join playernew p on p.matricule = zp2.player_key

                                                    join zt_result_type rt on rt.result_type_id = zgp.result_type_id

                                                    where t.iscurrent = 1

                                                    and g.visit_player_list_id = zpl.player_list_id

                                                    and zpl2.player_list_id = g.home_player_list_id

                                                    and g.group_id = ".$groupId."

                                                    and p.elo_calcul <> 0

                                                    and zp.player_key = '".$item->playerKey."'";

                                                $visitMatchesResult = mysql_query($query, $connect);

                                                rejectTranIfError($query, "calculate_stats.php: selecteren van de uitpartijen is mislukt.");

                                                while ($visitMatchRow = mysql_fetch_assoc($visitMatchesResult)) {

                                                    $visitMatch = new Match;

                                                    $visitMatch->playerKey = $visitMatchRow['player_key'];

                                                    $visitMatch->opponentRating = $visitMatchRow['elo_calcul'];

                                                    $visitMatch->result = $visitMatchRow['result'];

                                                    $visitMatch->homeMatch = 0;

                                                    array_push($matches, $visitMatch);    

                                                }

                                                

                                                // Rp = Ra + performance change where Ra is the average opponent rating excluding

                                                // players w/o any rating

                                                

                                                $opponentRating = 0;

                                                $playedAgainstRated = 0;

                                                $totalAgainstRated = 0.0;

                                                foreach ($matches as $match) {

                                                    $opponentRating += $match->opponentRating;

                                                    $playedAgainstRated++;

                                                    if ($match->homeMatch == 1) {

                                                        if (strnatcasecmp("1 - 0", $match->result) == 0) {

                                                            $totalAgainstRated += 1;    

                                                        } else if (strnatcasecmp("5 - 5", $match->result) == 0) {

                                                            $totalAgainstRated += 0.5;

                                                        } else if (strnatcasecmp("1 - FF", $match->result) == 0) {

                                                            $totalAgainstRated += 1;

                                                        }

                                                    } else {

                                                        if (strnatcasecmp("5 - 5", $match->result) == 0) {

                                                            $totalAgainstRated += 0.5;    

                                                        } else if (strnatcasecmp("0 - 1", $match->result) == 0) {

                                                            $totalAgainstRated += 1;

                                                        } else if (strnatcasecmp("FF - 1", $match->result) == 0) {

                                                            $totalAgainstRated += 1;   

                                                        }

                                                    }    

                                                }

                                                

                                                if ($playedAgainstRated == 0) {

                                                    $gem = $opponentRating / $item->played; 

                                                } else {

                                                    $gem = $opponentRating / $playedAgainstRated; 

                                                }

                                                

                                                if ($playedAgainstRated == $totalAgainstRated || $totalAgainstRated == 0) {

                                                    $tpr = "-";

                                                    //$tpr = $gem + 400 * ($item->won - $item->lost) / $item->played

                                                } else {

                                                    

                                                    $p = round($totalAgainstRated / $playedAgainstRated, 2);

                                                    $dp = $ratingPerformanceTable["".number_format($p,2).""];

                                                    $tpr = round($gem + $dp);

                                                }

                                                 

                                        ?>                                                                                 

                                        <tr>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$index?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->playerKey?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><a class="underline" href="player_detail.php?dep=<?=$dep?>&playerKey=<?=$item->playerKey?>"><?=$item->playerName?></a></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->playerClub?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->playerElo?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->won?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->drawn?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->lost?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->played?></td>

                                            <td style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$item->total?></td>

                                            <td align="center" style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=$tpr?></td>

                                            <td align="center" style="border-bottom:#DDDDDD solid 1px" class="<?=$css?>"><?=round($gem)?></td>

                                        </tr>

                                        <?php

                                            }

                                        ?>

                                    </table>

								</td>

							</tr>

						</table>

					</td>

				</tr>

				<tr>

			</table>

		</td>

	</tr>

</table>



                                                     

<?php include("footer.php"); ?>

